<?php
/**
 * La classe de base pour les déplacements
 *
 * @version 2.0
 * @author jonathan poelen <jonathan.poelen@gmail.com>
 * @date 02-10-2010
 */
abstract class SW_Move
{
	/** @var int*/
	protected $destination;

	/** @var int*/
	protected $duration;

	/** @var string*/
	protected $orderName = 'move';

	/** @var SW_Removable|null */
	protected $removable;


	/**
	 * Méthode à déclencher quand l'armé est arrivé à destination
	 * @param SW_Units $army
	 */
	abstract public function arrive(SW_Units $units);


	/**
	 * @param int $destination
	 * @param SW_Removable $removable = null
	 * @param int $time = null
	 * @param SW_Move $next = null
	 */
	public function __construct($destination, SW_Removable $removable = null, $time = null, SW_Move $next = null)
	{
		$this->destination = $destination;
		$this->removable = $removable;
		$this->time = $time;
		$this->next = $next;
	}

	/**
	 * @return SW_Removable|null
	 */
	public function getRemovable()
	{
		return $this->removable;
	}

	/**
	 * @param SW_Removable $removable
	 * @return SW_Removable|null : le SW_Removable précèdent
	 */
	public function setRemovable(SW_Removable $removable)
	{
		$tmp = $this->removable;
		$this->removable = $removable;
		return $tmp;
	}

	/**
	 * @param bool $force = true : si false et que removable n'est pas vide alors il n'est pas détruit et la fonction retourne false
	 * @return bool
	 */
	public function removeRemovable($force = true)
	{
		if (!$this->removable)
			return true;

		if (!$force && !$this->removable->isEmpty())
			return false;

		$this->removable = null;
		return true;
	}

	/**
	 * @return string
	 */
	public function getOrderName()
	{
		return (string)$this->orderName;
	}

	/**
	 * @return string
	 */
	public function __toString()
	{
		return $this->getOrderName();
	}

	/**
	 * @return int
	 */
	public function getDestination()
	{
		return $this->destination;
	}

	/**
	 * @param int $destination
	 */
	public function setDestination($destination)
	{
		$this->destination = $destination;
	}

	/**
	 * @param int $duration
	 */
	public function setDuration($duration)
	{
		$this->duration = $duration;
	}

	/**
	 * @return int
	 */
	public function getDuration()
	{
		return $this->duration;
	}

	/**
	 * @param int $time : au début du mouvement
	 * @param int $timeEnd = null : si null alors égal $_SERVER['REQUEST_TIME']
	 * @return bool
	 */
	public function isArrive($time, $timeEnd = null)
	{
		return $time + $this->duration  <= ($timeEnd === null ? $_SERVER['REQUEST_TIME'] : $timeEnd);
	}
}